<?php
/*
 * description：
 * author：wh
 * email：
 * createTime：{2024/6/24} {23:01} 
 */

namespace wanghua\general_utility_tools_php\tool;


use wanghua\general_utility_tools_php\http\Curl;
use wanghua\general_utility_tools_php\Mmodel;

class KuaidiTools
{
    /**
     * desc：查询物流平台
     * author：wh
     * @param string $num 物流单号
     * @return array
     */
    function getPlatform($num){
        return Mmodel::catch(function (){
            $url = "https://alayn.baidu.com/express/appdetail/get_com?num=315002419145908";
            $res = Curl::curl_get($url);
            if($res['code'] != 200){
                return Tools::set_res(500,'查询失败.'.$res['msg']);
            }
            $res_data = json_decode($res['data'],true);
            if($res_data['code']){
                return Tools::set_res(500,'查询失败.'.$res_data['message']);
            }
            return Tools::set_ok('ok',$res_data['data']);
        });
    }

    /**
     * desc：查询物流
     * author：wh
     * @return array
     */
    function query($num){
        return Mmodel::catch(function () use($num){
            $plt = $this->getPlatform($num);
            if($plt['code'] != 200){
                return Tools::set_res(500,'查询失败.'.$plt['msg']);
            }
            $company = $plt['data']['company'];
            $url = "https://alayn.baidu.com/express/appdetail/get_detail?query_from_srcid=51151&tokenV2=OLRFkDonXGP%2FQTmCKNu41zXJayuyUkfll0i7Tw5IWBFAsbTSXMycQooMPzAMO1Rc&appid=4001&nu={$num}&com={$company}&qid=4879176651996235000";
            $res = Curl::curl_get($url);
            if($res['code'] != 200){
                return Tools::set_res($res['code'],'请求失败.'.$res['msg']);
            }
            $res_data = json_decode($res['data'],true);
            if($res_data['error_code']){
                return Tools::set_res($res_data['error_code'],'查询失败.'.$res_data['msg']);
            }
            return Tools::set_ok('ok',$res_data['data']);
        });
    }

}